package com.tengju.user.infrastructure.repository.chief;

import com.tengju.user.domain.model.chief.*;
import com.tengju.user.domain.model.club.ClubId;
import com.tengju.user.domain.shared.IdObject;
import com.tengju.user.infrastructure.persistence.mybatis.dataobject.ChiefClubDO;
import com.tengju.user.infrastructure.persistence.mybatis.dataobject.ChiefClubRelationDO;
import com.tengju.user.infrastructure.persistence.mybatis.mapper.dealer.custom.ChiefClubMapper;
import com.tengju.user.infrastructure.repository.convert.ChiefClubConvert;
import com.tengju.user.infrastructure.repository.convert.ChiefClubRelationConvert;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Repository
public class ChiefClubRepositoryImpl implements ChiefClubRepository {

    @Resource
    ChiefClubMapper chiefClubMapper;

    @Override
    public List<ChiefClub> getChiefClub(ChiefId chiefId) {

        List<ChiefClubDO> chiefClubDOList = chiefClubMapper.queryByChiefId(chiefId.getValue());

        if (CollectionUtils.isEmpty(chiefClubDOList)) {
            return Collections.emptyList();
        }

        return chiefClubDOList.stream().map(ChiefClubConvert::convert).collect(Collectors.toList());
    }

    @Override
    public void batchInsert(List<ChiefClub> newChiefClubs) {
        if (CollectionUtils.isEmpty(newChiefClubs)) {
            return;
        }
        List<ChiefClubDO> chiefClubDOList = newChiefClubs.stream()
                .map(ChiefClubConvert::toDo)
                .collect(Collectors.toList());
        chiefClubMapper.batchInsert(chiefClubDOList);
    }

    @Override
    public void batchRemove(ChiefId chiefId, List<Long> clubIds) {
        if (CollectionUtils.isEmpty(clubIds)) {
            return;
        }
        chiefClubMapper.batchRemove(chiefId.getValue(), clubIds);
    }

    @Override
    public List<ChiefClubRelation> queryByClubIdAndChiefIdPage(ClubId clubId, ChiefId chiefId, Integer offset, Integer pageSize) {
        List<ChiefClubRelationDO> chiefClubRelationDOList = chiefClubMapper
                .queryByClubIdAndChiefId(Optional.ofNullable(clubId).map(IdObject::getValue).orElse(null),
                        Optional.ofNullable(chiefId).map(IdObject::getValue).orElse(null),
                        offset, pageSize);
        return chiefClubRelationDOList.stream().map(ChiefClubRelationConvert::convert).collect(Collectors.toList());
    }


    @Override
    public void removeChiefClubRelation(ChiefClubRelationId chiefClubRelationId) {
        if (chiefClubRelationId == null){
            return;
        }
        chiefClubMapper.removeChiefClubRelation(chiefClubRelationId.getValue());
    }

    @Override
    public int count(ClubId clubId, ChiefId chiefId) {
        return chiefClubMapper.count(Optional.ofNullable(clubId).map(IdObject::getValue).orElse(null),
                Optional.ofNullable(chiefId).map(IdObject::getValue).orElse(null));
    }

    @Override
    public void updateChiefClubRelation(ChiefClubUpdateRelation chiefClubUpdateRelation) {
        chiefClubMapper.updateChiefClubRelation(chiefClubUpdateRelation.getChiefClubRelationId().getValue(),
                chiefClubUpdateRelation.getChiefId().getValue(),
                chiefClubUpdateRelation.getClubId().getValue(),
                chiefClubUpdateRelation.getUpdateBy());
    }
}
